const mysql = require('mysql');

const pool = mysql.createPool({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'root123456',
    database: 'data_demo',
    connectionLimit: 15
})

const getConnection = () => new Promise(resolve => {
    pool.getConnection((err, connection) => {
        if (err) throw err;
        console.log('数据库连接成功...')
        resolve(connection)
    })
})

const query = (sql) => new Promise(async resolve => {
    const connection = await getConnection()
    connection.query(sql,(err, res)=>{
        if(err) throw err;
        resolve(res)
    })
})

module.exports = query